package com.zz.ecommerce.declareManage.exportB2CDeclare.cancelBillManage;

import com.alibaba.fastjson.JSONObject;
import com.zz.ecommerce.common.GridRequest;
import com.zz.ecommerce.common.GridResult;
import com.zz.ecommerce.common.constants.TableConstants;
import com.zz.ecommerce.declareManage.common.dao.*;
import com.zz.ecommerce.declareManage.common.service.CancelBillService;
import com.zz.ecommerce.pojo.*;
import com.zz.ecommerce.utils.SessionUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.UUID;

/**
 * Created by steven on 2016/3/30.
 */
@Controller
@RequestMapping("/exportB2C/cancelBill")
public class EB2cCancelBillController {

    @Autowired
    private ECancelBaseDao cancelBaseDao;

    @Autowired
    private CancelBillService billService;

    /**
     * 初始化核销单查询主页
     *
     * @return
     */
    @RequestMapping("/init")
    public String init() {
        return "declareManage/exportB2CDeclare/cancelBillManage/index";
    }

    /**
     * 查询核销基础信息列表
     *
     * @param base    核销基本信息
     * @param request
     * @param session
     * @return
     */
    @RequestMapping("/queryList")
    @ResponseBody
    public GridResult queryBaseInfoList(TECancelBase base, GridRequest request, HttpSession session) {
        base.setOperId(SessionUtil.getOperator(session));
        base.setOrgId(SessionUtil.getOrganizationId(session));
        return billService.queryBaseInfoList(request, base);
    }

    /**
     * 跳转到核销信息详情页面
     *
     * @param id       核销信息唯一序列号
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping("/showDetail")
    public String showDetail(@RequestParam(value = "id") String id, GridRequest request, ModelMap modelMap) {
        TECancelBase query = new TECancelBase();
        query.setId(id);
        TECancelBase baseInfo = cancelBaseDao.query(TableConstants.T_E_CANCEL_BASE, query);

        List<TECancelDeclare> declareList = billService.queryAllDeclareList(request, id);
        List<TECancelEndgood> endgoodList = billService.queryAllEndGoodsList(request, id);
        List<TECancelMaterials> materialsList = billService.queryAllMaterialsList(request, id);
        List<TECancelConsumption> consumptionList = billService.queryAllConsumptionList(request, id);

        modelMap.put("baseInfo", baseInfo);
        modelMap.put("declareListJSON", JSONObject.toJSONString(declareList));
        modelMap.put("endgoodListJSON", JSONObject.toJSONString(endgoodList));
        modelMap.put("materialsListJSON", JSONObject.toJSONString(materialsList));
        modelMap.put("consumptionListJSON", JSONObject.toJSONString(consumptionList));

        return "declareManage/exportB2CDeclare/cancelBillManage/showDetail";
    }

    /**
     * 跳转到新增界面
     *
     * @param modelMap
     * @return
     */
    @RequestMapping("/toAddPage")
    public String toAddPage(ModelMap modelMap) {

        return "declareManage/exportB2CDeclare/cancelBillManage/edit";
    }

    /**
     * 跳转到修改界面
     *
     * @param id       核销信息唯一序列号
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/toEditPage", method = {RequestMethod.GET, RequestMethod.POST})
    public String toEditPage(@RequestParam(value = "id", required = false) String id, ModelMap modelMap) {
        TECancelBase query = new TECancelBase();
        query.setId(id);
        modelMap.put("baseInfo", cancelBaseDao.query(TableConstants.T_E_CANCEL_BASE, query));

        return "declareManage/exportB2CDeclare/cancelBillManage/edit";
    }

    /**
     * 删除核销单信息
     *
     * @param base 核销信息
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public boolean deleteCancelBillInfo(TECancelBase base) {
        return billService.deleteCancelBillInfo(base);
    }

    /**
     * 新增核销单基本信息
     *
     * @param base 核销单基本信息
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public TECancelBase saveBaseInfo(TECancelBase base, HttpSession session) {
        if (StringUtils.isNotEmpty(base.getId())) {
            TECancelBase query = new TECancelBase();
            query.setId(base.getId());
            TECancelBase base1 = cancelBaseDao.query(TableConstants.T_E_CANCEL_BASE, query);
            if (null != base1) {
                cancelBaseDao.update(TableConstants.T_E_CANCEL_BASE, base, " WHERE ID = #{updateObj.id}");
            } else {
                base.setOrgId(SessionUtil.getOrganizationId(session));
                base.setOperId(SessionUtil.getOperator(session));
                base.setSendStatus("E1");
                cancelBaseDao.insert(TableConstants.T_E_CANCEL_BASE, base);
            }
        } else {
            base.setId(UUID.randomUUID().toString());
            base.setOrgId(SessionUtil.getOrganizationId(session));
            base.setOperId(SessionUtil.getOperator(session));
            base.setSendStatus("E1");
            cancelBaseDao.insert(TableConstants.T_E_CANCEL_BASE, base);
        }

        return base;
    }

    /**
     * 新增核销单关联的单损耗单信息
     *
     * @param type     类型：区分查询统计的表
     * @param relateId 核销单关联Id
     * @return
     */
    @RequestMapping("/getNextSeqNo")
    @ResponseBody
    public int getNextSeqNo(@RequestParam("type") String type, @RequestParam("") String relateId) {
        String tableName = "";
        switch (type) {
            case "0":
                tableName = TableConstants.T_E_CANCEL_CONSUMPTION;
                break;
            case "1":
                tableName = TableConstants.T_E_CANCEL_ENDGOOD;
                break;
            case "2":
                tableName = TableConstants.T_E_CANCEL_MATERIALS;
                break;
        }

        return StringUtils.isEmpty(tableName) ? 1 : billService.getNextSeqNo(tableName, relateId);
    }
}
